package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.constant.MessageConstant;
import org.dey.entity.Result;
import org.dey.mapper.QuestionBankMapper;
import org.dey.pojo.PageVo;
import org.dey.pojo.QuestionBank;
import org.dey.service.QuestionBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank> implements QuestionBankService {

    @Autowired
    QuestionBankMapper questionBankMapper;

    @Override
    public PageVo<QuestionBank> queryQuestionBank(QuestionBank questionBank, int pageNum, int pagesize) {
        QueryWrapper<QuestionBank> queryWrapper = new QueryWrapper<QuestionBank>();
        if(StringUtils.isNotBlank(questionBank.getName())){
            queryWrapper.like("name",questionBank.getName());
        }

        PageHelper.startPage(pageNum, pagesize);

        List<QuestionBank> list = this.list(queryWrapper);

        PageInfo<QuestionBank> pageInfo = new PageInfo<QuestionBank>(list);


        PageVo<QuestionBank> pageVo = new PageVo<QuestionBank>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return  pageVo;
    }

    @Override
    public Result insertQuestionBank(QuestionBank questionBank) {
        Result result = null;

        QueryWrapper<QuestionBank> queryWrapper = new QueryWrapper<QuestionBank>();
        queryWrapper.eq("name", questionBank.getName());

        boolean one = this.save(questionBank);

        if(one){
            result = new Result(true, MessageConstant.ADD_QUESTION_BANK_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.ADD_QUESTION_BANK_FAIL);
        }

        return result;
    }

    @Override
    public Result deleteQuestionBank(int id) {

        Result result =null;
        boolean b = this.removeById(id);
        if(b){
            result = new Result(true, MessageConstant.DELETE_QUESTION_BANK_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.DELETE_QUESTION_BANK_FAIL);
        }
        return result;
    }

    @Override
    public QuestionBank queryQuestionBankByid(int id) {

        return this.getById(id);
    }

    @Override
    public Result updateQuestionBank(QuestionBank questionBank) {

        Result result =null;
        boolean b = this.updateById(questionBank);
        if(b){
            result = new Result(true, MessageConstant.EDIT_QUESTION_BANK_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.EDIT_QUESTION_BANK_FAIL);
        }
        return result;
    }
}
